﻿namespace QLPhongKham.BaseClass.BenhNhan
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoBNTiepDon
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deidql;
        private int _iiddangnhap;
        private int _iiddoituong;
        private int _iidphong;
        private int _istt;
        private string _scannang;
        private string _shuyetap;
        private string _slydokham;
        private string _smabn;
        private string _smach;
        private string _sngay;
        private string _snhietdo;
        private string _sSchema;
        private string _stuoivao;

        public dtoBNTiepDon()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoBNTiepDon(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "bn_tiepdon.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where  ", columnIDQL, " = ", this._deidql }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "bn_tiepdon.cs");
                return false;
            }
        }

        public decimal GetIDInsert()
        {
            decimal iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "bn_tiepdon.cs");
                iDInsert = 1M;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public decimal GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(idql) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                decimal d = 0M;
                try
                {
                    d = decimal.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return (d++);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "bn_tiepdon.cs");
                return 1M;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "bn_tiepdon.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnIDQL, ",", columnMABN, ",", columnNGAY, ",", columnIDDOITUONG, ",", columnIDPHONG, ",", columnSTT, 
                    ",", columnLYDOKHAM, ",", columnMACH, ",", columnNHIETDO, ",", columnHUYETAP, ",", columnCANNANG, ",", columnTUOIVAO, ",", columnIDDANGNHAP, ") values(", this._deidql, 
                    ",'", this._smabn, "',to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", this._iiddoituong, ",", this._iidphong, ",", this._istt, ",:", columnLYDOKHAM, ",'", this._smach, "','", this._snhietdo, 
                    "','", this._shuyetap, "','", this._scannang, "','", this._stuoivao, "',", this._iiddangnhap, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnLYDOKHAM, NpgsqlDbType.Text, 200).Value = this._slydokham;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "bn_tiepdon.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deidql = 0L;
            this._smabn = "";
            this._sngay = "";
            this._iiddoituong = 0;
            this._iidphong = 0;
            this._istt = 0;
            this._slydokham = "";
            this._smach = "";
            this._snhietdo = "";
            this._shuyetap = "";
            this._scannang = "";
            this._stuoivao = "";
            this._iiddangnhap = 0;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "bn_tiepdon.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnIDQL, ",", columnMABN, ",to_char(", columnNGAY, ",'dd/mm/yyyy hh24:mi') as ", columnNGAY, ",", columnIDDOITUONG, ",", columnIDPHONG, ",", columnSTT, ",", columnLYDOKHAM, 
                    ",", columnMACH, ",", columnNHIETDO, ",", columnHUYETAP, ",", columnCANNANG, ",", columnTUOIVAO, ",", columnIDDANGNHAP, " from ", this._sSchema, ".", tableName, 
                    " where ", columnIDQL, " = ", this._deidql
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deidql = long.Parse(dataTable.Rows[0][columnIDQL].ToString());
                    this._smabn = dataTable.Rows[0][columnMABN].ToString();
                    this._sngay = dataTable.Rows[0][columnNGAY].ToString();
                    this._iiddoituong = int.Parse(dataTable.Rows[0][columnIDDOITUONG].ToString());
                    this._iidphong = int.Parse(dataTable.Rows[0][columnIDPHONG].ToString());
                    this._istt = int.Parse(dataTable.Rows[0][columnSTT].ToString());
                    this._slydokham = dataTable.Rows[0][columnLYDOKHAM].ToString();
                    this._smach = dataTable.Rows[0][columnMACH].ToString();
                    this._snhietdo = dataTable.Rows[0][columnNHIETDO].ToString();
                    this._shuyetap = dataTable.Rows[0][columnHUYETAP].ToString();
                    this._scannang = dataTable.Rows[0][columnCANNANG].ToString();
                    this._stuoivao = dataTable.Rows[0][columnTUOIVAO].ToString();
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "bn_tiepdon.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, "");
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "bn_tiepdon.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnIDQL + "," + columnMABN + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnIDDOITUONG + "," + columnIDPHONG + "," + columnSTT + "," + columnLYDOKHAM + "," + columnMACH + "," + columnNHIETDO + "," + columnHUYETAP + "," + columnCANNANG + "," + columnTUOIVAO + "," + columnIDDANGNHAP + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "bn_tiepdon.cs");
                return null;
            }
        }

        public DataTable LoadAll(string mabn)
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, mabn);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "bn_tiepdon.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect, string mabn)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnIDQL + "," + columnMABN + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnIDDOITUONG + "," + columnIDPHONG + "," + columnSTT + "," + columnLYDOKHAM + "," + columnMACH + "," + columnNHIETDO + "," + columnHUYETAP + "," + columnCANNANG + "," + columnTUOIVAO + "," + columnIDDANGNHAP + " from " + this._sSchema + "." + tableName + " where 1=1" + ((mabn == "") ? "" : (" and mabn='" + mabn + "'")), connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "bn_tiepdon.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "bn_tiepdon.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnMABN, " = '", this._smabn, "',", columnNGAY, " = to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", columnIDDOITUONG, " = ", this._iiddoituong, 
                    ",", columnIDPHONG, " = ", this._iidphong, ",", columnSTT, " = ", this._istt, ",", columnLYDOKHAM, " = :", columnLYDOKHAM, ",", columnMACH, " = '", this._smach, 
                    "',", columnNHIETDO, " = '", this._snhietdo, "',", columnHUYETAP, " = '", this._shuyetap, "',", columnCANNANG, " = '", this._scannang, "',,", columnIDDANGNHAP, " = ", this._iiddangnhap, 
                    ",", columnTUOIVAO, " = '", this._stuoivao, "' where ", columnIDQL, " = ", this._deidql
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnLYDOKHAM, NpgsqlDbType.Text, 200).Value = this._slydokham;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "bn_tiepdon.cs");
                return false;
            }
        }

        public static string columnCANNANG
        {
            get
            {
                return "cannang";
            }
        }

        public static string columnHUYETAP
        {
            get
            {
                return "huyetap";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDDOITUONG
        {
            get
            {
                return "iddoituong";
            }
        }

        public static string columnIDPHONG
        {
            get
            {
                return "idphong";
            }
        }

        public static string columnIDQL
        {
            get
            {
                return "idql";
            }
        }

        public static string columnLYDOKHAM
        {
            get
            {
                return "lydokham";
            }
        }

        public static string columnMABN
        {
            get
            {
                return "mabn";
            }
        }

        public static string columnMACH
        {
            get
            {
                return "mach";
            }
        }

        public static string columnNGAY
        {
            get
            {
                return "ngay";
            }
        }

        public static string columnNHIETDO
        {
            get
            {
                return "nhietdo";
            }
        }

        public static string columnSTT
        {
            get
            {
                return "stt";
            }
        }

        public static string columnTUOIVAO
        {
            get
            {
                return "tuoivao";
            }
        }

        public string pCANNANG
        {
            get
            {
                return this._scannang;
            }
            set
            {
                this._scannang = value;
            }
        }

        public string pHUYETAP
        {
            get
            {
                return this._shuyetap;
            }
            set
            {
                this._shuyetap = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDDOITUONG
        {
            get
            {
                return this._iiddoituong;
            }
            set
            {
                this._iiddoituong = value;
            }
        }

        public int pIDPHONG
        {
            get
            {
                return this._iidphong;
            }
            set
            {
                this._iidphong = value;
            }
        }

        public long pIDQL
        {
            get
            {
                return this._deidql;
            }
            set
            {
                this._deidql = value;
            }
        }

        public string pLYDOKHAM
        {
            get
            {
                return this._slydokham;
            }
            set
            {
                this._slydokham = value;
            }
        }

        public string pMABN
        {
            get
            {
                return this._smabn;
            }
            set
            {
                this._smabn = value;
            }
        }

        public string pMACH
        {
            get
            {
                return this._smach;
            }
            set
            {
                this._smach = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngay;
            }
            set
            {
                this._sngay = value;
            }
        }

        public string pNHIETDO
        {
            get
            {
                return this._snhietdo;
            }
            set
            {
                this._snhietdo = value;
            }
        }

        public int pSTT
        {
            get
            {
                return this._istt;
            }
            set
            {
                this._istt = value;
            }
        }

        public string pTUOIVAO
        {
            get
            {
                return this._stuoivao;
            }
            set
            {
                this._stuoivao = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "bn_tiepdon";
            }
        }
    }
}

